package com.ruoyi.house_sale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.Ticket;
import com.ruoyi.house_sale.service.ITicketService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 票据管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-19
 */
@RestController
@RequestMapping("/house_sale/Ticket")
public class TicketController extends BaseController
{
    @Autowired
    private ITicketService ticketService;

    /**
     * 查询票据管理列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ticket ticket)
    {
        startPage();
        List<Ticket> list = ticketService.selectTicketList(ticket);
        return getDataTable(list);
    }

//    @GetMapping("/NextNo/{ticketno}")
//    public AjaxResult NextNo(@PathVariable("ticketno") String ticketno)
//    {
//        Long nextno = ticketService.findNextNo(ticketno);
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("nextno", nextno);
//        return ajax;
//    }
    @GetMapping("/NextNo1/{ticketid}")
    public AjaxResult NextNo1(@PathVariable("ticketid") Long ticketid)
    {
        Long nextno = ticketService.findNextNo(ticketid);
        return toAjax(ticketService.updNextNo(ticketid,nextno+1));
    }
    /**
     * 导出票据管理列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:export')")
    @Log(title = "票据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ticket ticket)
    {
        List<Ticket> list = ticketService.selectTicketList(ticket);
        ExcelUtil<Ticket> util = new ExcelUtil<Ticket>(Ticket.class);
        util.exportExcel(response, list, "票据管理数据");
    }

    /**
     * 获取票据管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:query')")
    @GetMapping(value = "/{ticketid}")
    public AjaxResult getInfo(@PathVariable("ticketid") Long ticketid)
    {
        return AjaxResult.success(ticketService.selectTicketByTicketid(ticketid));
    }

    /**
     * 新增票据管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:add')")
    @Log(title = "票据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ticket ticket)
    {
        return toAjax(ticketService.insertTicket(ticket));
    }

    /**
     * 修改票据管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:edit')")
    @Log(title = "票据管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ticket ticket)
    {
        return toAjax(ticketService.updateTicket(ticket));
    }

    /**
     * 删除票据管理
     */
    @PreAuthorize("@ss.hasPermi('house_sale:Ticket:remove')")
    @Log(title = "票据管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ticketids}")
    public AjaxResult remove(@PathVariable Long[] ticketids)
    {
        return toAjax(ticketService.deleteTicketByTicketids(ticketids));
    }
}
